<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\ProductLimit;
use Illuminate\Http\Request;

class BannerController extends ApiBaseController
{
    //
    public function commonUploadFile(Request $request)
    {

        $img = request()->file('post_file');
        if (!$img) {
            return error('图片为空');
        }
        $extension = $img->getClientOriginalExtension();
        $filename = $img->getClientOriginalName();
        // 验证只能上传图片文件
        $allow_upload_ext = ['png', 'jpg', 'jpeg'];
        if (!in_array($extension, $allow_upload_ext)) {
            return  error('请上传图片文件' . json_encode($allow_upload_ext));
        }
        $newFileName = md5($filename . time() . mt_rand(1, 1000)) . '.' . $extension;
        $savePath = 'uploads/post_file/' . $newFileName;
        $img->storePubliclyAs('uploads/post_file/', $newFileName, ['disk' => 'public']);
        return success([
            "data" => [
                "path" => "/storage/" . $savePath,
            ]
        ]);
    }
    public function getList(Request $request)
    {
        $params = $request->all();
        $list = Banner::orderBy('id', 'desc')->paginate($this->pageSize);
        return success(['data' => page_format($list)]);
    }
    public function add(Request $request)
    {
        $params = $request->all();
        // 上传图片
        $res = Banner::create($params);
        return success();
    }
    public function change(Request $request)
    {
        $params = $request->only(['path', 'url', 'status']);
        // 上传图片
        $res = Banner::where('id', $params['id'])->update($params);
        return success();
    }
    public function del(Request $request)
    {
        $id = request('id');
        $res = Banner::destroy($id);
        return  success();
    }
}
